﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using XNA_Game_Engine.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using MenesDroid;

namespace XNA_Game_Engine.Canvas.Level_Selection
{

    class LevelSlotsFrame : Frame
    {

        Texture2D levelSelectionText = null;
        Texture2D lockedLevelTexture = null;
        Texture2D lockedLevelFrame = null;

        bool showLockedLevelFrame;
        int lockedLevelFrameTimer;
        int timeToVanish = 1000;

        public LevelSlotsFrame(Game game, int x, int y, int frameWidth, int frameHeight)
            : base( game, x, y, frameWidth, frameHeight, false )
        {
            _enabled = true;

            showLockedLevelFrame = false;
            lockedLevelFrameTimer = 0;
        }

        public override void LoadContent(ContentManager content)
        {
            lockedLevelTexture = content.Load<Texture2D>(@"Level_Selection_Screen\level_locked");
            lockedLevelFrame = content.Load<Texture2D>(@"Level_Selection_Screen\fase_bloqueada");

            int espacamento = 0;
            int buttonSize = (FrameWidth / 5);
            
            int buttonX = X + espacamento;
            int buttonY = Y + 150;

            
            Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_1_button"),
                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_1_button"),
                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                     ActionsHandler.LoadLevel01 ));

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 2)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_2_button"),
                                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_2_button"),
                                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                     ActionsHandler.LoadLevel02));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 3)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_3_button"),
                                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_3_button"),
                                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                     ActionsHandler.LoadLevel03));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 4)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_4_button"),
                                                    content.Load<Texture2D>(@"Level_Selection_Screen\level_4_button"),
                                                    new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                    ActionsHandler.LoadLevel04));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 5)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_5_button"),
                                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_5_button"),
                                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                     ActionsHandler.LoadLevel05));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }


            /**
             * Segunda Linha
             */
            buttonX = X + espacamento;
            buttonY += buttonSize;
            if (ActionsHandler.unlockedLevels >= 6)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_6_button"),
                                                    content.Load<Texture2D>(@"Level_Selection_Screen\level_6_button"),
                                                    new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                    ActionsHandler.LoadLevel06));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 7)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_7_button"),
                                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_7_button"),
                                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                     ActionsHandler.LoadLevel07));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 8)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_8_button"),
                                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_8_button"),
                                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                     ActionsHandler.LoadLevel08));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 9)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_9_button"),
                                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_9_button"),
                                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                     ActionsHandler.LoadLevel09));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }

            buttonX += buttonSize;
            if (ActionsHandler.unlockedLevels >= 10)
            {
                Buttons.Add(new clsButton(content.Load<Texture2D>(@"Level_Selection_Screen\level_10_button"),
                                                     content.Load<Texture2D>(@"Level_Selection_Screen\level_10_button"),
                                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                                     ActionsHandler.LoadLevel10));
            }
            else
            {
                createLockedLevelButton(buttonX, buttonY, buttonSize);
            }


            levelSelectionText = content.Load<Texture2D>(@"Level_Selection_Screen\selecao_de_fases");

            base.LoadContent(content);
        }


        public override void Update(GameTime gameTime)
        {
            if (showLockedLevelFrame)
            {
                lockedLevelFrameTimer += gameTime.ElapsedGameTime.Milliseconds;
                if (lockedLevelFrameTimer >= timeToVanish)
                {
                    showLockedLevelFrame = false;
                }
            }
            base.Update(gameTime);
        }


        public override void Render(SpriteBatch renderTarget)
        {            
            base.Render(renderTarget);
            renderTarget.Draw(levelSelectionText, new Vector2(X + (FrameWidth / 2 - levelSelectionText.Width / 2), Y + 25), Color.White);
            
            if (showLockedLevelFrame)
                renderTarget.Draw(lockedLevelFrame, new Vector2(X + (FrameWidth / 2 - lockedLevelFrame.Width / 2), Y + FrameHeight/2 - lockedLevelFrame.Height/2), Color.White);
        }


        private void createLockedLevelButton( int buttonX, int buttonY, int buttonSize)
        {
            Buttons.Add(new clsButton( lockedLevelTexture, lockedLevelTexture,
                                     new Vector2(buttonX, buttonY), new Vector2(buttonSize, buttonSize),
                                     LockedLevel ) );
        }


        public void LockedLevel(object sender, EventArgs e)
        {
            showLockedLevelFrame = true;
            lockedLevelFrameTimer = 0;
        }
    }
}
